<?php

/**
 * @author: biggege
 * @since:  2021-08-27 11:57:10
 */
namespace App\Validations\Manage;

use App\Helpers\ManageValidator;
use App\Models\Question;
use App\Models\QuestionGroup;
use App\Models\QuestionAnswer;
use Validator;

trait QuestionValidator
{
    use ManageValidator;

    protected $validatorAttributes = [
        'question_group_id' => '问题所属组',
        'question_group_module' => '问题所属模块',
        'question_name' => '问题名称',
        'question_type' => '问题类型',
        'question_choice' => '问题选项',
        'question_status' => '问题状态',
        'question_sort' => '问题排序',
    ];

    // 更新
    private function updateValidation(array $data) {
        $validator = Validator::make($data, [
            'question_group_id' => 'required|integer',
            'question_group_module' => 'required|string',
            'question_name' => 'required|string|max:200',
            'question_type' => 'required|integer|min:1|max:3',
            'question_choice' => 'required_if:question_type,'.Question::QUESTION_SINGLE.','.Question::QUESTION_MULTIPLE.'|array',
            'question_choice.*' => 'required|string',
        ], $this->validatorMessage, $this->validatorAttributes);
        if ($validator->fails()) {
            $errors = $validator->errors()->first(); // all()
            return $this->error(400, $errors); // implode(' | ', $errors)
        }

        // 问题所属模块
        if(in_array($data['question_group_module'], Question::getModular())) {
            return $this->error(400, '问题所属模块不存在');
        }
    }













}